"use strict";
/**
 * @author zhaojiuyi
 * @date 2022/5/30-10:21
 * @version 1.0.0
 */
import api from '@/network/api';
import store from '@/store/index';

export default {
    data() {
        return {
            rules: {
                username: [{
                    required: true,
                    message: '手机号不能为空',
                    trigger: 'blur'
                }, {
                    min: 11,
                    max: 11,
                    message: '请输入11位手机号',
                    trigger: 'blur'
                }],
                password: [{
                    required: true,
                    message: '密码不能为空',
                    trigger: 'blur'
                }]
            },
            checked: true,
            loginForm: {
                username: '',
                password: ''
            },
            loading: false
        }
    },
    mounted() {
        //绑定事件，其他界面可能要写在methods里
        window.addEventListener('keydown', this.keyDown);
    },
    methods: {
        keyDown(e) {
            //如果是按回车则执行登录方法
            if (e.keyCode == 13) {
                this.login('loginForm')
            }
        },
        login(loginform) {
            var _this = this;
            let logindata = {
                'phone': this.loginForm.username,
                'password': this.loginForm.password
            };
            this.$refs[loginform].validate((valid) => {
                if (valid) {
                    api.login(logindata).then(res => {
                        if (res.code === 200) {
                            let user = this.$jsonToParse(res); //深拷贝
                            store.commit("userId", user['profile']['userId']);
                            _this.$router.replace({
                                path: '/'
                            });
                        } else {
                            this.$alert(res.msg, '提示', {
                                confirmButtonText: '确定'
                            })
                        }
                    })
                }
            });
        }
    },
}